package com.mby.utils;

import com.mby.entity.Dealer;
import com.mby.entity.DeviceToken;
import com.mby.entity.User;
import com.mongodb.*;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by noah on 2018/5/3.
 */
public class CsAppUtil {

    private static MongoClient client;
    private static DB db;
    private static DBCollection collection;

    public static DeviceToken getDeviceToken(String user_id,Integer brand_id) throws Exception{
        getCsMongo();
        collection = db.getCollection("device_tokens");
        BasicDBObject cond = new BasicDBObject();
        cond.put("user_id",Integer.parseInt(user_id.replace("C","")));
        cond.put("brand_id",brand_id);
        DBObject device_token = collection.findOne(cond);
        DeviceToken dt = null;
        if(device_token != null){
            dt = new DeviceToken();
            dt.setPlatform(device_token.get("platform").toString());
            dt.setDeviceToken(device_token.get("device_token").toString());
            return dt;
        }
        return dt;
    }

    public static User getUserInfo(String vin) throws Exception{
        getCsMongo();
        collection = db.getCollection("vehicles");
        BasicDBObject cond = new BasicDBObject();
        cond.put("vin",vin);
        DBObject vehicle = collection.findOne(cond);
        User user = null;
        if(vehicle != null){
            user = new User();
            if(vehicle.get("user_id") != null){
                user.setId((Integer) vehicle.get("user_id"));
            }
            if(vehicle.get("brand_id") != null){
                user.setBrandId((Integer) vehicle.get("brand_id"));
            }
        }
        return user;
    }

    public static Dealer getDealer(String dealerCode) throws Exception{
        getCsMongo();
        Dealer d = new Dealer();
        collection = db.getCollection("dealers");
        BasicDBObject cond = new BasicDBObject();
        cond.put("code",dealerCode);
        DBObject dealer = collection.findOne(cond);
        if(dealer != null){
            d.setIntId((Integer)dealer.get("_id"));
            d.setName(dealer.get("name").toString());
        }
        return d;
    }

    public static Dealer getDealer(Integer intId) throws Exception{
        getCsMongo();
        Dealer d = new Dealer();
        collection = db.getCollection("dealers");
        BasicDBObject cond = new BasicDBObject();
        cond.put("_id",intId);
        DBObject dealer = collection.findOne(cond);
        if(dealer != null){
            d.setId(dealer.get("code").toString());
            d.setName(dealer.get("name").toString());
        }
        return d;
    }

    private static void getCsMongo() throws Exception{
        //todo 需要配置多个数据源
        System.out.print("连接cs数据库"+WebConstant.CS_DB_NAME+".................");
        ServerAddress serverAddress = new ServerAddress(WebConstant.CS_DB_PATH, WebConstant.CS_DB_PORT);
        List<ServerAddress> address = new ArrayList<>();
        address.add(serverAddress);
        List<MongoCredential> credentials = new ArrayList<>();
        client = new MongoClient(address, credentials);
        db = client.getDB(WebConstant.CS_DB_NAME);
        System.out.println("成功");
    }
}
